package com.springboot.study.service;

import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

/**
 * @Author: guodong
 * @Date: 2021/9/16 10:56
 * @Version: 1.0
 * @Description:
 */
@Slf4j
@Service
public class DemoRetryService implements IRetryDemoService{

    @Override
    @Retryable(maxAttempts = 3, backoff = @Backoff(value = 3000, multiplier = 1.5), recover = "recover")
    public void call() throws Exception {
        System.out.println("do something...");
        int a = 1 / 0;
        System.out.println(a);
        throw new ArithmeticException("RPC调用异常");
    }

    @Recover
    public void recover(ArithmeticException e) {
        System.out.println("111111");
        System.out.println(e.getMessage());
    }

//    @Retryable(maxAttempts = 3, backoff = @Backoff(value = 3000, multiplier = 1.5))
//    public Customer getCustomer(String customerId) {
//        if (true) {
//            JSONArray data = retObj.getJSONArray("data");
//            if (data != null && !data.isEmpty()) {
//                return data.toJavaList(Customer.class).get(0);
//            }
//        } else {
//            log.error("异常，{}", customerId);
//            throw new RuntimeException("获数据失败");
//        }
//        return null;
//    }

}